package com.hydl.power.service.impl;

import com.hydl.power.mapper.PowerDataEnergyMapper;
import com.hydl.power.mapper.ProjectMapper;
import com.hydl.power.mapper.TemperatureDataMapper;
import com.hydl.power.mapper.TemperatureDeviceMapper;
import com.hydl.power.pojo.PowerConsume;
import com.hydl.power.pojo.PowerDataEnergy;
import com.hydl.power.pojo.Project;
import com.hydl.power.service.PowerDataEnergyService;
import io.micrometer.core.instrument.util.StringUtils;
import lombok.SneakyThrows;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author wxb
 * @date 2020/4/23 16:00
 */
@Service
public class PowerDataEnergyServiceImpl implements PowerDataEnergyService {
    @Autowired
    private PowerDataEnergyMapper energyMapper;

    @Autowired
    private ProjectMapper projectMapper;

    @Autowired
    private TemperatureDataMapper temperatureDataMapper;

    @Autowired
    private TemperatureDeviceMapper temperatureDeviceMapper;


    /**
     * 日报
     * @param createTime
     * @param areaId
     * @param projectId
     * @param powerRoomId
     * @return
     */
    @Override
    public List<List<PowerConsume>>  listByDay(String createTime, String areaId, String projectId,String powerRoomId) {

       List<List<PowerConsume>> powerConsumeList=new ArrayList<>();
        if(!StringUtils.isNotBlank(createTime)){
            createTime = new SimpleDateFormat("yyyy-MM-dd").format(new Date())+"%";
        }else{
            createTime=createTime+"%";
        }
        Project project = projectMapper.queryProjectByProId(projectId);
        if (project.getPowerDaraFlag().equals("1")) {
            //根据地区id项目id配电房id查询设备
            List<PowerConsume> powerConsumes = energyMapper.dayPower(createTime, areaId, projectId, powerRoomId, project.getTableName());
            if (powerConsumes.size() > 0) {
                //根据地区项目配电房查询设备的设备号
                List<String> deviceIdList = energyMapper.getDeviceId(areaId, projectId, powerRoomId);
                for (String deviceId : deviceIdList) {
                    List<PowerConsume> list = new ArrayList();
                    for (PowerConsume powerConsume : powerConsumes) {
                        if (deviceId.equals(powerConsume.getDeviceId())) {
                            list.add(powerConsume);
                        }
                    }
                    if (list.size() > 0) {
                        powerConsumeList.add(list);
                    }
                }
                for (List<PowerConsume> list:powerConsumeList){
                    for (int i=0;i<list.size(); i++){
                        PowerConsume powerConsume = list.get(i);
                        if (i+1<list.size()){
                            powerConsume.setForwardActiveTotalElectricalEnergy(list.get(i+1).getMinForwardActiveTotalElectricalEnergy()-powerConsume.getMinForwardActiveTotalElectricalEnergy());
                        }else{
                            powerConsume.setForwardActiveTotalElectricalEnergy(powerConsume.getMaxForwardActiveTotalElectricalEnergy()-powerConsume.getMinForwardActiveTotalElectricalEnergy());
                        }
                    }
                }
            }


        }else{
            //根据地区id项目id配电房id查询设备
            List<PowerConsume> powerConsumes = temperatureDataMapper.dayPower(createTime, areaId, projectId, powerRoomId, project.getTemperatureDataTableName());
            if (powerConsumes.size() > 0) {
                //根据地区项目配电房查询设备的设备号
                List<String> deviceIdList = temperatureDeviceMapper.getDeviceId(areaId, projectId, powerRoomId);
                for (String deviceId : deviceIdList) {
                    List<PowerConsume> list = new ArrayList();
                    for (PowerConsume powerConsume : powerConsumes) {
                        if (deviceId.equals(powerConsume.getDeviceId())) {
                            list.add(powerConsume);
                        }
                    }
                    if (list.size() > 0) {
                        powerConsumeList.add(list);
                    }
                }
                for (List<PowerConsume> list:powerConsumeList){
                    for (int i=0;i<list.size();i++){
                        if (i+1<list.size()){
                            list.get(i).setForwardActiveTotalElectricalEnergy(list.get(i+1).getMinForwardActiveTotalElectricalEnergy()-list.get(i).getMinForwardActiveTotalElectricalEnergy());
                        }else {
                            list.get(i).setForwardActiveTotalElectricalEnergy(list.get(i).getMaxForwardActiveTotalElectricalEnergy()-list.get(i).getMinForwardActiveTotalElectricalEnergy());
                        }
                    }

                }
            }


        }
        return powerConsumeList;

    }



    /**
     * 月报
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param areaId    地区ID
     * @param projectId 项目ID
     * @param powerRoomId 配电房ID
     * @return
     */
    @SneakyThrows
    @Override
    public List<List<PowerConsume>> listByMonth(String startTime, String areaId, String projectId, String powerRoomId,String endTime) throws ParseException {
        List<List<PowerConsume>> powerConsumeList=new ArrayList<>();
        Project project = projectMapper.queryProjectByProId(projectId);

        if(project.getPowerDaraFlag().equals("1")){
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Calendar calendar  =   Calendar.getInstance();
            if (calendar.get(Calendar.DAY_OF_MONTH)<=simpleDateFormat.parse(endTime).getDate()){
                calendar.setTime(simpleDateFormat.parse(endTime)); //需要将date数据转移到Calender对象中操作
                calendar.add(calendar.DATE, 1);//把日期往后增加n天.正数往后推,负数往前移动
                Date time = calendar.getTime();//这个时间就是日期往后推一天的结果
                List<PowerConsume> powerConsumes = energyMapper.listByMonth(startTime,areaId,projectId,powerRoomId,project.getTableName(),simpleDateFormat.format(time));

                if(powerConsumes.size()>0){
                    //根据地区项目配电房查询设备的设备号
                    List<String> deviceIdList = energyMapper.getDeviceId(areaId, projectId, powerRoomId);
                    for(String deviceId:deviceIdList){
                        List<PowerConsume> list=new ArrayList();
                        for (PowerConsume powerConsume:powerConsumes){
                            if(deviceId.equals(powerConsume.getDeviceId())){
                                powerConsume.setTime(powerConsume.getTime().substring(powerConsume.getTime().lastIndexOf("-")+1));
                                list.add(powerConsume);
                            }

                        }
                        if(list.size()>0){
                            powerConsumeList.add(list);
                        }

                    }
                    for (List<PowerConsume> list:powerConsumeList){
                        for (int i=0;i<list.size(); i++){
                            PowerConsume powerConsume = list.get(i);
                            if (i+1<list.size()){
                                powerConsume.setForwardActiveTotalElectricalEnergy(list.get(i+1).getMinForwardActiveTotalElectricalEnergy()-powerConsume.getMinForwardActiveTotalElectricalEnergy());
                            }else{
                                powerConsume.setForwardActiveTotalElectricalEnergy(powerConsume.getMaxForwardActiveTotalElectricalEnergy()-powerConsume.getMinForwardActiveTotalElectricalEnergy());
                            }
                        }
                    }
                }

            }else{
                calendar.setTime(simpleDateFormat.parse(endTime)); //需要将date数据转移到Calender对象中操作
                calendar.add(calendar.DATE, 2);//把日期往后增加n天.正数往后推,负数往前移动
                Date time = calendar.getTime();//这个时间就是日期往后推一天的结果
                List<PowerConsume> powerConsumes = energyMapper.listByMonth(startTime,areaId,projectId,powerRoomId,project.getTableName(),simpleDateFormat.format(time));

                if(powerConsumes.size()>0){
                    //根据地区项目配电房查询设备的设备号
                    List<String> deviceIdList = energyMapper.getDeviceId(areaId, projectId, powerRoomId);
                    for(String deviceId:deviceIdList){
                        List<PowerConsume> list=new ArrayList();
                        for (PowerConsume powerConsume:powerConsumes){
                            if(deviceId.equals(powerConsume.getDeviceId())){
                                powerConsume.setTime(powerConsume.getTime().substring(powerConsume.getTime().lastIndexOf("-")+1));
                                list.add(powerConsume);
                            }

                        }
                        if(list.size()>0){
                            powerConsumeList.add(list);
                        }

                    }
                    for (List<PowerConsume> list:powerConsumeList){
                        for (int i=0;i<list.size(); i++){
                            PowerConsume powerConsume = list.get(i);
                            if (i+1<list.size()){
                                powerConsume.setForwardActiveTotalElectricalEnergy(list.get(i+1).getMinForwardActiveTotalElectricalEnergy()-powerConsume.getMinForwardActiveTotalElectricalEnergy());
                            }else{
                                list.remove(i);
                            }
                        }
                    }
                }


            }
        }else{
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Calendar calendar  =   Calendar.getInstance();
            if (calendar.get(Calendar.DAY_OF_MONTH)<=simpleDateFormat.parse(endTime).getDate()){
                calendar.setTime(simpleDateFormat.parse(endTime)); //需要将date数据转移到Calender对象中操作
                calendar.add(calendar.DATE, 1);//把日期往后增加n天.正数往后推,负数往前移动
                Date time = calendar.getTime();//这个时间就是日期往后推一天的结果

                //根据地区id项目id配电房id查询设备
                List<PowerConsume> powerConsumes = temperatureDataMapper.listByMonth(startTime,areaId,projectId,powerRoomId,project.getTemperatureDataTableName(),simpleDateFormat.format(time));
                if(powerConsumes.size()>0){
                    //根据地区项目配电房查询设备的设备号
                    List<String> deviceIdList = temperatureDeviceMapper.getDeviceId(areaId, projectId, powerRoomId);
                    for(String deviceId:deviceIdList){
                        List<PowerConsume> list=new ArrayList();
                        for (PowerConsume powerConsume:powerConsumes){
                            if(deviceId.equals(powerConsume.getDeviceId())){
                                powerConsume.setTime(powerConsume.getTime().substring(powerConsume.getTime().lastIndexOf("-")+1));
                                list.add(powerConsume);
                            }

                        }
                        if(list.size()>0){
                            powerConsumeList.add(list);
                        }

                    }
                    for (List<PowerConsume> list:powerConsumeList){
                        for (int i=0;i<list.size(); i++){
                            PowerConsume powerConsume = list.get(i);
                            if (i+1<list.size()){
                                powerConsume.setForwardActiveTotalElectricalEnergy(list.get(i+1).getMinForwardActiveTotalElectricalEnergy()-powerConsume.getMinForwardActiveTotalElectricalEnergy());
                            }else{
                                powerConsume.setForwardActiveTotalElectricalEnergy(powerConsume.getMaxForwardActiveTotalElectricalEnergy()-powerConsume.getMinForwardActiveTotalElectricalEnergy());
                            }
                        }
                    }
                }

            }else{
                calendar.setTime(simpleDateFormat.parse(endTime)); //需要将date数据转移到Calender对象中操作
                calendar.add(calendar.DATE, 2);//把日期往后增加n天.正数往后推,负数往前移动
                Date time = calendar.getTime();//这个时间就是日期往后推一天的结果

                //根据地区id项目id配电房id查询设备
                List<PowerConsume> powerConsumes = temperatureDataMapper.listByMonth(startTime,areaId,projectId,powerRoomId,project.getTemperatureDataTableName(),simpleDateFormat.format(time));
                if(powerConsumes.size()>0){
                    //根据地区项目配电房查询设备的设备号
                    List<String> deviceIdList = temperatureDeviceMapper.getDeviceId(areaId, projectId, powerRoomId);
                    for(String deviceId:deviceIdList){
                        List<PowerConsume> list=new ArrayList();
                        for (PowerConsume powerConsume:powerConsumes){
                            if(deviceId.equals(powerConsume.getDeviceId())){
                                powerConsume.setTime(powerConsume.getTime().substring(powerConsume.getTime().lastIndexOf("-")+1));
                                list.add(powerConsume);
                            }

                        }
                        if(list.size()>0){
                            powerConsumeList.add(list);
                        }

                    }
                    for (List<PowerConsume> list:powerConsumeList){
                        for (int i=0;i<list.size(); i++){
                            PowerConsume powerConsume = list.get(i);
                            if (i+1<list.size()){
                                powerConsume.setForwardActiveTotalElectricalEnergy(list.get(i+1).getMinForwardActiveTotalElectricalEnergy()-powerConsume.getMinForwardActiveTotalElectricalEnergy());
                            }else{
                                list.remove(i);
                            }
                        }
                    }
                }


            }
        }
        return  powerConsumeList;

    }

    /**
     * 年报
     * @param startTime
     * @param areaId
     * @param projectId
     * @param powerRoomId
     * @return
     */
    @Override
    public List<List<PowerConsume>> listByYear(String startTime, String areaId, String projectId, String powerRoomId) {
        List<List<PowerConsume>> powerConsumeList=new ArrayList<>();
        if(!StringUtils.isNotBlank(startTime)){
            startTime = new SimpleDateFormat("yyyy").format(new Date())+"%";
        }else{
            startTime=startTime+"%";
        }
        Project project = projectMapper.queryProjectByProId(projectId);
        if (project.getPowerDaraFlag().equals("1")){

            //根据地区id项目id配电房id查询设备
            List<PowerConsume> powerConsumes =  energyMapper.listByYear(startTime,areaId,projectId,powerRoomId,project.getTableName());

            if(powerConsumes.size()>0){
                //根据地区项目配电房查询设备的设备号
                List<String> deviceIdList=energyMapper.getDeviceId(areaId,projectId,powerRoomId);
                for(String deviceId:deviceIdList){
                    List<PowerConsume> list=new ArrayList<>();
                    for (PowerConsume powerConsume:powerConsumes){
                        if(deviceId.equals(powerConsume.getDeviceId())){
                            list.add(powerConsume);
                        }
                    }
                    if(list.size()>0){
                        powerConsumeList.add(list);
                    }
                }
                for (List<PowerConsume> list:powerConsumeList){
                    for (int i=0;i<list.size(); i++){
                        PowerConsume powerConsume = list.get(i);
                        if (i+1<list.size()){
                            powerConsume.setForwardActiveTotalElectricalEnergy(list.get(i+1).getMinForwardActiveTotalElectricalEnergy()-powerConsume.getMinForwardActiveTotalElectricalEnergy());
                        }else{
                            powerConsume.setForwardActiveTotalElectricalEnergy(powerConsume.getMaxForwardActiveTotalElectricalEnergy()-powerConsume.getMinForwardActiveTotalElectricalEnergy());
                        }
                    }
                }
            }

        }else{
            //根据地区id项目id配电房id查询设备
            List<PowerConsume> powerConsumes =  temperatureDataMapper.listByYear(startTime,areaId,projectId,powerRoomId,project.getTemperatureDataTableName());
            if(powerConsumes.size()>0){
                //根据地区项目配电房查询设备的设备号
                List<String> deviceIdList=temperatureDeviceMapper.getDeviceId(areaId,projectId,powerRoomId);
                for(String deviceId:deviceIdList){
                    List<PowerConsume> list=new ArrayList();
                    for (PowerConsume powerConsume:powerConsumes){
                        if(deviceId.equals(powerConsume.getDeviceId())){
                            list.add(powerConsume);
                        }
                    }
                    if(list.size()>0){
                        powerConsumeList.add(list);
                    }

                }
                for (List<PowerConsume> list:powerConsumeList){
                    for (int i=0;i<list.size(); i++){
                        PowerConsume powerConsume = list.get(i);
                        if (i+1<list.size()){
                            powerConsume.setForwardActiveTotalElectricalEnergy(list.get(i+1).getMinForwardActiveTotalElectricalEnergy()-powerConsume.getMinForwardActiveTotalElectricalEnergy());
                        }else{
                            powerConsume.setForwardActiveTotalElectricalEnergy(powerConsume.getMaxForwardActiveTotalElectricalEnergy()-powerConsume.getMinForwardActiveTotalElectricalEnergy());
                        }
                    }
                }



            }

        }
        return powerConsumeList;
    }

    /**
     * 周报
     * @param createTime
     * @param areaId
     * @param projectId
     * @param powerRoomId
     * @return
     */
    @Override
    public List<List<PowerConsume>> listByWeek(String createTime,String areaId,String projectId,String powerRoomId) throws ParseException {
        List<List<PowerConsume>> powerConsumeList=new ArrayList<>();
        Project project = projectMapper.queryProjectByProId(projectId);
        if (project.getPowerDaraFlag().equals("1")){
            //根据地区项目配电房查询设备的设备号
            List<String> deviceIdList=energyMapper.getDeviceId(areaId,projectId,powerRoomId);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            if(!StringUtils.isNotBlank(createTime)) {
                Date time = new Date();
                List<Date> days = dateToWeek(time);
                createTime = sdf.format(days.get(0));
                Calendar  calendar=new GregorianCalendar();
                calendar.setTime(days.get(6));
                calendar.add(calendar.DATE,1);
                String endTime = sdf.format(calendar.getTime());

                List<PowerConsume> powerConsumes = energyMapper.listByWeek(createTime,endTime,areaId,projectId,powerRoomId);
                if(powerConsumes.size()>0){
                    for(String deviceId:deviceIdList){
                        List<PowerConsume> list=new ArrayList();
                        for (PowerConsume powerConsume:powerConsumes){
                            if(deviceId.equals(powerConsume.getDeviceId())){
                                list.add(powerConsume);
                            }
                        }
                        if (list.size()>0){
                            powerConsumeList.add(list);
                        }

                    }
/*                    for (List<PowerConsume> list:powerConsumeList){
                        for (int i=0;i<list.size(); i++){
                            PowerConsume powerConsume = list.get(i);
                            if (i+1<list.size()){
                                powerConsume.setForwardActiveTotalElectricalEnergy(list.get(i+1).getMinForwardActiveTotalElectricalEnergy()-powerConsume.getMinForwardActiveTotalElectricalEnergy());
                            }else{
                                powerConsume.setForwardActiveTotalElectricalEnergy(powerConsume.getMaxForwardActiveTotalElectricalEnergy()-powerConsume.getMinForwardActiveTotalElectricalEnergy());
                            }
                        }
                    }*/
                }

            }else{
                List<Date> days = dateToWeek(new SimpleDateFormat("yyyy-MM-dd").parse(createTime));
                String startTime = sdf.format(days.get(0));
                Calendar  calendar=new GregorianCalendar();
                calendar.setTime(days.get(6));
                calendar.add(calendar.DATE,1);
                String endTime = sdf.format(calendar.getTime());
                List<PowerConsume> powerConsumes = energyMapper.listByWeek(startTime,endTime,areaId,projectId,powerRoomId);
                if (powerConsumes.size()>0){
                    for(String deviceId:deviceIdList){
                        List<PowerConsume> list=new ArrayList();
                        for (PowerConsume powerConsume:powerConsumes){
                            if(deviceId.equals(powerConsume.getDeviceId())){
                                list.add(powerConsume);
                            }
                        }
                        if (list.size()>0){
                            powerConsumeList.add(list);
                        }
                    }
/*                    for (List<PowerConsume> list:powerConsumeList){
                        for (int i=0;i<list.size(); i++){
                            PowerConsume powerConsume = list.get(i);
                            if (i+1<list.size()){
                                powerConsume.setForwardActiveTotalElectricalEnergy(list.get(i+1).getMinForwardActiveTotalElectricalEnergy()-powerConsume.getMinForwardActiveTotalElectricalEnergy());
                            }else{
                                powerConsume.setForwardActiveTotalElectricalEnergy(powerConsume.getMaxForwardActiveTotalElectricalEnergy()-powerConsume.getMinForwardActiveTotalElectricalEnergy());
                            }
                        }
                    }*/
                }


            }
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            List<Date> days = dateToWeek(simpleDateFormat.parse(createTime));
            for (List<PowerConsume> powerConsumes:powerConsumeList){
                for (Date date:days) {
                    int flag=0;
                    String deviceName="";
                    String deviceId="";
                    for (PowerConsume powerConsume:powerConsumes){
                        deviceName=powerConsume.getDeviceName();
                        deviceId=powerConsume.getDeviceId();
                        if (simpleDateFormat.format(date).equals(powerConsume.getTime())) {
                            flag=1;
                            break;
                        }
                    }
                    if (flag==0){
                        if (org.apache.commons.lang3.StringUtils.isNotBlank(deviceName)){
                            PowerConsume powerConsume=new PowerConsume();
                            powerConsume.setDeviceName(deviceName);
                            powerConsume.setDeviceId(deviceId);
                            powerConsume.setForwardActiveTotalElectricalEnergy(-1f);
                            powerConsume.setTime(simpleDateFormat.format(date));
                            powerConsumes.add(powerConsume);
                        }
                    }
                }

            }
            return powerConsumeList;
        }else {
            //根据地区项目配电房查询设备的设备号
            List<String> deviceIdList=temperatureDeviceMapper.getDeviceId(areaId,projectId,powerRoomId);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            if(!StringUtils.isNotBlank(createTime)) {
                Date time = new Date();
                List<Date> days = dateToWeek(time);
                createTime = sdf.format(days.get(0));
                Calendar  calendar=new GregorianCalendar();
                calendar.setTime(days.get(6));
                calendar.add(calendar.DATE,1);
                String endTime = sdf.format(calendar.getTime());

                List<PowerConsume> powerConsumes = energyMapper.listByWeek(createTime,endTime,areaId,projectId,powerRoomId);
                if(powerConsumes.size()>0){
                    for(String deviceId:deviceIdList){
                        List<PowerConsume> list=new ArrayList();
                        for (PowerConsume powerConsume:powerConsumes){
                            if(deviceId.equals(powerConsume.getDeviceId())){
                                list.add(powerConsume);
                            }
                        }
                        if (list.size()>0){
                            powerConsumeList.add(list);
                        }

                    }
/*                    for (List<PowerConsume> list:powerConsumeList){
                        for (int i=0;i<list.size(); i++){
                            PowerConsume powerConsume = list.get(i);
                            if (i+1<list.size()){
                                powerConsume.setForwardActiveTotalElectricalEnergy(list.get(i+1).getMinForwardActiveTotalElectricalEnergy()-powerConsume.getMinForwardActiveTotalElectricalEnergy());
                            }else{
                                powerConsume.setForwardActiveTotalElectricalEnergy(powerConsume.getMaxForwardActiveTotalElectricalEnergy()-powerConsume.getMinForwardActiveTotalElectricalEnergy());
                            }
                        }
                    }*/
                }


            }else{
                List<Date> days = dateToWeek(new SimpleDateFormat("yyyy-MM-dd").parse(createTime));
                String startTime = sdf.format(days.get(0));
                Calendar  calendar=new GregorianCalendar();
                calendar.setTime(days.get(6));
                calendar.add(calendar.DATE,1);
                String endTime = sdf.format(calendar.getTime());
                List<PowerConsume> powerConsumes = energyMapper.listByWeek(startTime,endTime,areaId,projectId,powerRoomId);
                if (powerConsumes.size()>0){
                    for(String deviceId:deviceIdList){
                        List<PowerConsume> list=new ArrayList();
                        for (PowerConsume powerConsume:powerConsumes){
                            if(deviceId.equals(powerConsume.getDeviceId())){
                                list.add(powerConsume);
                            }
                        }
                        if (list.size()>0){
                            powerConsumeList.add(list);
                        }
                    }
/*                    for (List<PowerConsume> list:powerConsumeList){
                        for (int i=0;i<list.size(); i++){
                            PowerConsume powerConsume = list.get(i);
                            if (i+1<list.size()){
                                powerConsume.setForwardActiveTotalElectricalEnergy(list.get(i+1).getMinForwardActiveTotalElectricalEnergy()-powerConsume.getMinForwardActiveTotalElectricalEnergy());
                            }else{
                                powerConsume.setForwardActiveTotalElectricalEnergy(powerConsume.getMaxForwardActiveTotalElectricalEnergy()-powerConsume.getMinForwardActiveTotalElectricalEnergy());
                            }
                        }
                    }*/
                }

            }
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            List<Date> days = dateToWeek(simpleDateFormat.parse(createTime));
            for (List<PowerConsume> powerConsumes:powerConsumeList){
                for (Date date:days) {
                    int flag=0;
                    String deviceName="";
                    String deviceId="";
                    for (PowerConsume powerConsume:powerConsumes){
                        deviceName=powerConsume.getDeviceName();
                        deviceId=powerConsume.getDeviceId();
                        if (simpleDateFormat.format(date).equals(powerConsume.getTime())) {
                            flag=1;
                            break;
                        }
                    }
                    if (flag==0){
                        if (org.apache.commons.lang3.StringUtils.isNotBlank(deviceName)){
                            PowerConsume powerConsume=new PowerConsume();
                            powerConsume.setDeviceName(deviceName);
                            powerConsume.setDeviceId(deviceId);
                            powerConsume.setForwardActiveTotalElectricalEnergy(-1f);
                            powerConsume.setTime(simpleDateFormat.format(date));
                            System.out.println(powerConsume);
                            powerConsumes.add(powerConsume);
                        }
                    }
                }
            }
            return powerConsumeList;
        }

    }

    /**
     * 根据日期获得所在周的日期
     * @param mdate
     * @return
     */
    public static List<Date> dateToWeek(Date mdate) {
        Calendar cal = Calendar.getInstance();
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        cal.setTime(mdate);
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
        if(dayWeek==1){
            dayWeek = 8;
        }
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - dayWeek);
        Date mondayDate = cal.getTime();
        cal.add(Calendar.DATE, 4 +cal.getFirstDayOfWeek());
        Date sundayDate = cal.getTime();
        List<Date> lDate = new ArrayList<>();
        lDate.add(mondayDate);
        Calendar calBegin = Calendar.getInstance();
        calBegin.setTime(mondayDate);
        Calendar calEnd = Calendar.getInstance();
        calEnd.setTime(sundayDate);
        while (sundayDate.after(calBegin.getTime())) {
            calBegin.add(Calendar.DAY_OF_MONTH, 1);
            lDate.add(calBegin.getTime());
        }
        return lDate;
    }



    @Override
    public List<PowerDataEnergy> listByPde(Long begin, Long end, String areaId, String projectId,String powerRoomId, String deviceId,Integer flag) {
        List<PowerDataEnergy> lists = new ArrayList<>();
        if (flag == 1){
            List<PowerDataEnergy> list = energyMapper.listPtByList(begin, end, areaId, projectId,powerRoomId, deviceId);
            return list;
        }else if (flag == 2){
            List<PowerDataEnergy> list = energyMapper.listDlByList(begin, end, areaId, projectId,powerRoomId, deviceId);
            return list;
        }else if (flag == 3){
            List<PowerDataEnergy> list = energyMapper.listXiangDyByList(begin, end, areaId, projectId,powerRoomId, deviceId);
            return list;
        }else if (flag == 4){
            List<PowerDataEnergy> list = energyMapper.listXianDyByList(begin, end, areaId, projectId,powerRoomId, deviceId);
            return list;
        }else if (flag == 5){
            List<PowerDataEnergy> list = energyMapper.listPfByList(begin, end, areaId, projectId,powerRoomId, deviceId);
            return list;
        }else {
            return lists;
        }
    }

    @Override
    public List<PowerDataEnergy> listPtByList(Long begin, Long end, String areaId, String projectId, String powerRoomId, String deviceId) {
        return energyMapper.listPtByList(begin, end, areaId, projectId, powerRoomId, deviceId);
    }
    @Override
    public List<PowerDataEnergy> listDlByList(Long begin, Long end, String areaId, String projectId, String powerRoomId, String deviceId) {
        return energyMapper.listDlByList(begin, end, areaId, projectId, powerRoomId, deviceId);

    }
    @Override
    public List<PowerDataEnergy> listXiangDyByList(Long begin, Long end, String areaId, String projectId, String powerRoomId, String deviceId) {
        return energyMapper.listXiangDyByList(begin, end, areaId, projectId, powerRoomId, deviceId);
    }
    @Override
    public List<PowerDataEnergy> listXianDyByList(Long begin, Long end, String areaId, String projectId, String powerRoomId, String deviceId) {
        return energyMapper.listXianDyByList(begin, end, areaId, projectId, powerRoomId, deviceId);
    }
    @Override
    public List<PowerDataEnergy> listPfByList(Long begin, Long end, String areaId, String projectId, String powerRoomId, String deviceId) {
        return energyMapper.listPfByList(begin, end, areaId, projectId, powerRoomId, deviceId);
    }

    @Override
    public List<List<PowerConsume>> listByMonths(String startTime, String areaId, String projectId, String powerRoomId, String endTime) throws ParseException {
        List<List<PowerConsume>> list = new ArrayList<>();
        //日期加一天
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar  =   Calendar.getInstance();
        calendar.setTime(simpleDateFormat.parse(endTime)); //需要将date数据转移到Calender对象中操作
        calendar.add(calendar.DATE, 1);//把日期往后增加n天.正数往后推,负数往前移动
        Date time = calendar.getTime();//这个时间就是日期往后推一天的结果
        List<PowerConsume> list1 =  energyMapper.listByMonths(startTime,areaId,projectId,powerRoomId,simpleDateFormat.format(time));
        Project project = projectMapper.queryProjectByProId(projectId);
        List<String> deviceIdList=new ArrayList<>();
        if(list1.size()>0){
            if(project.getPowerDaraFlag().equals("1")){
//根据地区项目配电房查询设备的设备号
                 deviceIdList = energyMapper.getDeviceId(areaId, projectId, powerRoomId);
            }else {
                //根据地区项目配电房查询设备的设备号
                deviceIdList = temperatureDeviceMapper.getDeviceId(areaId, projectId, powerRoomId);
            }
           for(String deviceId:deviceIdList){
                List<PowerConsume> list2=new ArrayList();
                for (PowerConsume powerConsume:list1){
                    if(deviceId.equals(powerConsume.getDeviceId())){
                        list2.add(powerConsume);
                    }
                }
                if(list2.size()>0){
                    list.add(list2);
                    list2=new ArrayList<>();
                }

            }
        }
        return list;
    }


}
